package com.plant.serviceImpl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import com.plant.dao.BaseDao;
import com.plant.entity.UgcComment;
import com.plant.entity.UgcLike;
import com.plant.entity.UgcUser;
import com.plant.service.HomePageService;


@Service
@Transactional
public class HomePageServiceImpl implements HomePageService{
	@Resource(name = "baseDaoImpl") 
	private BaseDao basedao;

	public boolean AddLikes(UgcLike ul) {//----1.用户点赞
		return basedao.add(ul);
	}

	@SuppressWarnings("unchecked")
	public List<Object> LikesID(Integer user_id,Integer content_type,Integer content_id,Integer current_user_id) {//---2.判断是否点赞
		String sql="SELECT id from ugc_like where user_id=("+user_id+") and to_user_id=("+current_user_id+") and to_content_type=("+content_type+") and to_content_id=("+content_id+")";
		return basedao.Bysql(sql, UgcLike.class);
	}

	public boolean DeleteLikes(UgcLike ul) {//---3.用户取消点赞
		return basedao.del(ul);
	}

	public boolean AddComment(UgcComment uc) {//---4.用户添加评论
		return basedao.add(uc);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String,Object>> NoFriendUserInf(Object obj) {//---随机推荐3个陌生人
		String hql="SELECT id,nickname,photo FROM ugc_user WHERE id NOT IN ("+obj+") ORDER BY rand() LIMIT 3";
		return basedao.Bysql(hql, null);
	}

	public boolean CheckLikes(UgcUser u,Object objuid,Object objcontentid) {//---判断用户是否点赞
		String hql="select count(id) from UgcLike where userId=("+u.getId()+") and toUserId=("+objuid+") and toContentType=1 and toContentId="+objcontentid+"";
		long l=basedao.CountHql(hql);
		if(l==0){
			boolean b=false;
			return b;
		}else{
			return true;
		}
	}
	public boolean CheckLikesI(UgcUser u,Integer objuid,Object objcontentid) {//---判断用户是否点赞
		String hql="select count(id) from UgcLike where userId=("+u.getId()+") and toUserId=("+objuid+") and toContentType=1 and toContentId="+objcontentid+"";
		long l=basedao.CountHql(hql);
		if(l==0){
			boolean b=false;
			return b;
		}else{
			return true;
		}
	}
	public boolean CheckTonpicJoinLikes(UgcUser u,Object objuid,Object objcontentid) {//---判断用户是否点赞
		String hql="select count(id) from UgcLike where userId=("+u.getId()+") and toUserId=("+objuid+") and toContentType=4 and toContentId="+objcontentid+"";
		long l=basedao.CountHql(hql);
		if(l==0){
			boolean b=false;
			return b;
		}else{
			return true;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> LikesList(Object ids,Object objcontentid) {//-----查询所有点赞用户信息
		String sql="select b.id,b.nickname,b.photo from ugc_like as a,ugc_user as b where a.user_id=b.id AND a.to_content_id=("+objcontentid+") and a.user_id in("+ids+") and a.to_content_type=1 order by a.create_time desc";
		return basedao.Bysql(sql, UgcUser.class);
	}
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> LikesListI(Object objcontentid) {//-----查询所有点赞用户信息
		String sql="select b.id user_id,b.nickname user_name,b.photo user_photo from ugc_like as a,ugc_user as b where a.user_id=b.id AND a.to_content_id=("+objcontentid+") and to_content_type=1 order by a.create_time desc";
		return basedao.Bysql(sql, null);
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> TopicLikesList(Object ids,Object objcontentid) {//-----话题参与点赞列表
		String sql="select b.id,b.nickname,b.photo from ugc_like as a,ugc_user as b where a.user_id=b.id AND a.to_content_id=("+objcontentid+") and a.user_id in ("+ids+") and a.to_content_type=4";
		return basedao.Bysql(sql, UgcUser.class);
	}


	@SuppressWarnings("unchecked")
	public List<Object[]> CommentList(Object ids,Object obj) {//查询新鲜事评论列表
		String sql="SELECT a.id id_key,a.user_id from_user_id,a.content,a.to_content_user_id to_user_id,b.nickname from_user_name,a.reply_comment_user_id,a.create_time from ugc_comment as a,ugc_user as b where a.user_id=b.id AND a.to_content_id=("+obj+") and a.user_id in ("+ids+") and to_content_type=1";
		return basedao.Bysql(sql, UgcComment.class);
	}
	@SuppressWarnings("unchecked")
	public List<Map<String,Object>> CommentListI(Object ids,Object obj) {//查询新鲜事评论列表
		String sql="SELECT a.id id_key,a.user_id from_user_id,a.content,a.to_content_user_id to_user_id,b.nickname from_user_name from ugc_comment as a,ugc_user as b where a.user_id=b.id AND a.to_content_id=("+obj+") and a.user_id in ("+ids+") and to_content_type=1";
		return basedao.Bysql(sql, null);
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> TopicCommentList(Object ids,Object obj) {//查看话题参与评论列表
		String sql="SELECT a.id,a.user_id,a.content,a.to_content_user_id,b.nickname,a.reply_comment_user_id from ugc_comment as a,ugc_user as b where a.user_id=b.id AND a.to_content_id=("+obj+") and a.user_id in ("+ids+") and to_content_type=4";
		return basedao.Bysql(sql, UgcComment.class);
	}
}
